<!-- 
	介绍：该页面为微信小程序用户授权登录页面是一个过度页面，微信公众号不需要用户授权获取用户信息则不需要显示具体页面信息
 -->
<template>
    <view>
        <view v-show="isLoading" class="content">
            <view class="zcHeader">
                <view class="posImg">
                    <image :src="imgPath+'UniImages/bg.png'" mode="widthFix" style="width: 100%;height: 100%;"></image>
                </view>
                <view class="head_info">
                    <view class="head_img">
                        <image :src="imgPath+'UniImages/logo.png'" mode="widthFix" style="width: 100%;height: 100%;"></image>
                    </view>
                    <view>新保险销售</view>
                </view>
            </view>
            <view class="zcCenter">
                <view class="zcCenter_title">登录授权后将获得以下权限</view>
                <view class="iconfont zcCenter_info">获得你的公开信息（昵称、头像、地区等）</view>
            </view>
            <checkbox-group @change="checkboxChange">
                <label class="cb_lab">
                    <checkbox value="1" color="#FFFFFF" style="transform: scale(0.5);" /><text>我已阅读并同意《用户协议》和《隐私政策》</text>
                </label>
            </checkbox-group>
            <button type="primary" :disabled="isDLogin" open-type="getUserInfo" lang="zh_CN" class="zcBut" @getuserinfo="doUserInfo">登录</button>

        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                isDLogin: true,
                isLoading: false,
                isLogin: true,
                type: '',
                imgPath: this.$ZcPublic.Domain
            }
        },
        onLoad(e) {
            let _this = this;
            // 判断用户操作的是否是注册，值为：register则注册，否则为登录，有上个页面传过来
            _this.type = e.type;
            if (e.isLoading == 'false') {
                _this.isLoading = true;
            }
            if (e.isLogin == 'true') {
                _this.isLogin = false;
            }

            // #ifdef H5
            // _this.isLoading = true;
            // H5 页面获取 openId ，需要参数 appid（微信公众号的 appId）、
            // action(获取到后台获取到 openID 后跳转相关页面的 参数判定，参数值的结构：[当前操作平台名称  _login] ，_login:是否登录，不传则默认注册) ，
            window.location.href = this.$ZcPublic.Domain + 'WXServer/getOpenId.ashx?appid=' + e.appid + '&action=' + e.action;
            // #endif

            // #ifdef MP-WEIXIN

            // 微信小程序登录、注册，以下是用户已授权获取用户信息的处理
            uni.getSetting({
                success(res) {
                    if (res.authSetting['scope.userInfo']) {
                        uni.getUserInfo({
                            lang: 'zh_CN',
                            success(resjson) {
                                if (!_this.isLogin) {
                                    return;
                                }
                                uni.login({
                                    provider: 'weixin',
                                    success: function(res1) {
                                        if (res1.code) {
                                            uni.showLoading({
                                                title: '加载中',
                                                mask: true
                                            });
                                            _this.$ZcPublic.Server.Post({
                                                IsNeedLogin: 'false', //根据需求而定
                                                Paras: {
                                                    Name: 'uni_Insure_Detail',
                                                    Action: 'Ext_getUserOpenId',
                                                    // 微信小程序获取 openId 参数详解
                                                    // AppId：微信小程序的 appId,是唯一的，可固定传入
                                                    // jsCode:微信小程序登录后获取到的 jsCode，用于后台获取 openId
                                                    // nickName、avatarUrl、gender、country、province、city、language：用户授权获取到的洪湖信息
                                                    Data: {
                                                        AppId: _this.$ZcPublic.AppId, //wx37eedfa0150a418a（保险）	// wx6244a8ffd9c53b36（众畅）
                                                        jsCode: res1.code,
                                                        nickName: resjson.userInfo.nickName,
                                                        avatarUrl: resjson.userInfo
                                                            .avatarUrl,
                                                        gender: resjson.userInfo.gender,
                                                        country: resjson.userInfo.country,
                                                        province: resjson.userInfo.province,
                                                        city: resjson.userInfo.city,
                                                        language: resjson.userInfo.language
                                                    }
                                                },
                                                Callback: _this.Callback_getLoginOpenId,
                                                Scope: _this
                                            })
                                        }
                                    }
                                });
                            }
                        })
                    } else {
                        _this.isLoading = true;
                    }
                }
            })
            // #endif


        },
        methods: {
            // 该函数为用户授权获取用户信息是的处理方式，
            doUserInfo(resjson) {
                let _this = this;
                if (resjson.detail.errMsg !== 'getUserInfo:ok') {
                    return;
                }
                uni.login({
                    provider: 'weixin',
                    success: function(res) {
                        console.log('code = ', res);
                        if (res.code) {
                            uni.showLoading({
                                title: '加载中'
                            });

                            _this.$ZcPublic.Server.Post({
                                IsNeedLogin: 'false', //根据需求而定
                                Paras: {
                                    Name: 'uni_Insure_Detail',
                                    Action: 'Ext_getUserOpenId',
                                    // UserId: '',//根据需求而定
                                    // 微信小程序获取 openId 参数详解
                                    // AppId：微信小程序的 appId,是唯一的，可固定传入
                                    // jsCode:微信小程序登录后获取到的 jsCode，用于后台获取 openId
                                    // nickName、avatarUrl、gender、country、province、city、language：用户授权获取到的洪湖信息
                                    Data: {
                                        AppId: _this.$ZcPublic.AppId,
                                        jsCode: res.code,
                                        nickName: resjson.detail.userInfo.nickName,
                                        avatarUrl: resjson.detail.userInfo.avatarUrl,
                                        gender: resjson.detail.userInfo.gender,
                                        country: resjson.detail.userInfo.country,
                                        province: resjson.detail.userInfo.province,
                                        city: resjson.detail.userInfo.city,
                                        language: resjson.detail.userInfo.language
                                    }
                                },
                                Callback: _this.Callback_getLoginOpenId,
                                Scope: _this
                            })
                        }
                    }
                });

            },

            // 获取到 openId 登录成功后，通过 回调参数 isLogin跳转处理
            Callback_getLoginOpenId(resjson, scope) {
                let _this = this;
                if (resjson.state === 'back') {
                    console.log('登录回调 = ', resjson);
                    if (!resjson.success) {
                        uni.showModal({
                            title: '温馨提示',
                            showCancel: false,
                            content: '' + resjson.msg
                        })
                        _this.isLoading = true;
                        return;
                    }

                    // 登录成功后，将参数存入缓存中，用于后面功能接口登录验证
                    uni.setStorageSync('person', resjson.data);
                    uni.setStorageSync('zcsoft_uni_insure_session', resjson.session);

                    if (resjson.data.isLogin === 'true') {
                        uni.switchTab({
                            url: '../home/home'
                        })
                    } else {
                        uni.reLaunch({
                            url: '../userBind/userBind'
                        })
                    }
                } else if (resjson.state === 'err') {
                    _this.isLogin = false;
                }
                if (resjson.state === 'end') {
                    uni.hideLoading();
                }

            },
            checkboxChange(res) {
                this.isDLogin = !this.isDLogin;
            }
        }
    }
</script>

<style>
    .content {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background-color: #FFFFFF;
        display: flex;
        flex-direction: column;
        /* justify-content: center; */
        align-items: center;
        /* padding: 60upx; */
    }

    .posImg {
        position: relative;
        width: 100%;
    }

    .head_info {
        position: absolute;
        bottom: 0;
    }

    .head_img {
        width: 200upx;
        height: 200upx;
        overflow: hidden;
        border-radius: 20%;
    }

    .head_info view:last-child {
        font-size: 36upx;
        color: #3CA4FA;
        display: flex;
        align-items: center;
        justify-content: center;
        letter-spacing: 6upx;
    }

    .zcHeader {
        display: flex;
        flex-direction: column;
        align-items: center;
        /* padding: 60upx; */
        font-size: 50upx;
        /* font-weight: bold; */
        padding-bottom: 140upx;
        /* border-bottom: 3upx solid #999999; */
        width: 100%;
        box-sizing: border-box;
        position: relative;
    }

    .cb_lab {
        display: flex;
        align-items: center;
        margin-bottom: 40upx;
        font-size: 30upx;
    }

    checkbox-group {
        width: auto !important;
    }

    checkbox[checked] .wx-checkbox-input {
        background-color: #3AA4FA;
    }


    .zcCenter {
        display: flex;
        flex-direction: column;
        padding: 60upx 0 40upx 0;
    }

    .zcCenter_title {
        font-weight: 600;
    }

    .zcCenter_info {
        font-weight: 600;
        color: #999999;
    }

    .zcCenter_info:before {
        content: '\e608';
        color: #3CA4FA;
    }

    .zcBut {
        background-color: #3ca4fa !important;
        padding: 20upx 0;
        font-size: 36upx;
        border-radius: 48upx;
        line-height: 1;
        width: 80%;
        margin-bottom: 40upx;
    }

    button[disabled] {
        background-color: #F5F5F5 !important;
        color: #999999 !important;
    }
</style>
